package com.tul.vapp.servicecenter.zookeeper;

import com.tul.cusa.common.util.PropertiesUtil;
import com.tul.cusa.zookeeper.IZookeeperClient;
import com.tul.cusa.zookeeper.ZkClientZookeeperClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ZookeeperClient工厂
 * 
 * @author wwxiang
 * @since 2017/12/28.
 */
public class ZooKeeperClientFactory {

	private static final Logger LOGGER = LoggerFactory.getLogger(ZooKeeperClientFactory.class);

	private static IZookeeperClient client = null;

	private static int port = 54304;

	/**
	 * 获取客户端
	 * 
	 * @return ZookeeperClient
	 */
	public static IZookeeperClient getClient() {
		if (client == null) {
			String zkAddress = null;
			try {
				zkAddress = PropertiesUtil.getPropertyByClassPath("common.properties", "dubbo.registry.address");
			} catch (Exception e) {
				LOGGER.warn("Can't get zookeeper address.");
			}

			if (zkAddress == null) {
				zkAddress = "127.0.0.1:" + port;
				LOGGER.warn("Zookeeper address use default " + zkAddress);
			}
			client = new ZkClientZookeeperClient(zkAddress);
		}
		return client;
	}

	public static void setPort(int serverPort) {
		port = serverPort;
	}

}
